﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Collections;
using System.Runtime.InteropServices;


namespace Krav_Player
{
    public partial class Player_Form1 : Form
    {

        Krav_Player Player = new Krav_Player();
        string[] files, paths;
       public string[] Musicas = new string[1000]; 
         


        public Player_Form1()
        {
            InitializeComponent();

            this.VOLUME.Text = Krav_Player.GetPlayer().Volume.ToString() + "%";
        }

        private void browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog = new OpenFileDialog();
            OpenFileDialog.InitialDirectory = Environment.SpecialFolder.MyMusic.ToString();
            OpenFileDialog.Filter = "*.mp3 файлы | *.mp3";
            OpenFileDialog.FilterIndex = 1;
            OpenFileDialog.Multiselect = true;
            if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
         
                files = OpenFileDialog.SafeFileNames;
                paths = OpenFileDialog.FileNames;
                for (int i = 0; i < files.Length; i++)
                {
                    Playlist.Items.Add(files[i]);


                }
               
                this.waybox.Text = OpenFileDialog.FileName.ToString();
               
               
                
            }

            
        }

        
        private void play_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (Krav_Player.GetPlayer().IsPaused())
                {
                    Krav_Player.GetPlayer().Open(paths[Playlist.SelectedIndex]);
                    Krav_Player.GetPlayer().Play(false);
                }

                else Krav_Player.GetPlayer().Play(paths[Playlist.SelectedIndex]);
                    
                    
                    timetotal.Text = String.Format("{0:00}:{1:00}",
                        Krav_Player.GetPlayer().Length / 60, Krav_Player.GetPlayer().Length % 60);
                trackBar1.Maximum = Krav_Player.GetPlayer().Length;
                
            
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void pause_Click(object sender, EventArgs e)
        {
            if (Krav_Player.GetPlayer().IsPlaying())
            {
                Krav_Player.GetPlayer().Pause();
            }
            else  Krav_Player.GetPlayer().Play(false);
             
        }

        private void stop_Click(object sender, EventArgs e)
        {
          
            if (Krav_Player.GetPlayer().IsPlaying() || Krav_Player.GetPlayer().IsPaused())
            {
                Krav_Player.GetPlayer().Close();
            }
            timecur.Text = String.Format("{0:00}:{1:00}", 0, 0);
            trackBar1.Value = 0;
            
            
            
        }

        private void volumeBar2_Scroll(object sender, EventArgs e)
        {
           
            Krav_Player.GetPlayer().Volume = this.volumeBar2.Value;
            this.VOLUME.Text = this.volumeBar2.Value.ToString() + "%";
            
        }

        private void rewback_Click(object sender, EventArgs e)
        {
           
            if (Player.Navigate > Player.Length - 5) Krav_Player.GetPlayer().Navigate -= 5;

        }

        private void rewforward_Click(object sender, EventArgs e)
        {
          
            if (Krav_Player.GetPlayer().Navigate < Krav_Player.GetPlayer().Length + 5) Krav_Player.GetPlayer().Navigate += 5;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Krav_Player.GetPlayer().Navigate = trackBar1.Value;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog = new OpenFileDialog();
            int pos;
            if (Krav_Player.GetPlayer().IsPlaying())
            {
                pos = Krav_Player.GetPlayer().Navigate;
                this.timecur.Text = String.Format("{0:00}:{1:00}", pos / 60, (pos) % 60);
                this.trackBar1.Value = pos;
                if (Player.Navigate == Player.Length - 1)
                {
                    try
                    {
                        Krav_Player.GetPlayer().Close();
                       
                        this.timecur.Text = String.Format("{0:00}:{1:00}", 0, 0);
                        this.trackBar1.Value = 0;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
        }

        private void waybox_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnPrevious_Click_1(object sender, EventArgs e)
        {
            if (Playlist.SelectedIndex > 0)
                Playlist.SelectedIndex = Playlist.SelectedIndex - 1;

          
            Krav_Player.GetPlayer().Play(paths[Playlist.SelectedIndex]);


            timetotal.Text = String.Format("{0:00}:{1:00}",
                Krav_Player.GetPlayer().Length / 60, Krav_Player.GetPlayer().Length % 60);
            trackBar1.Maximum = Krav_Player.GetPlayer().Length;
        }

        private void btnNext_Click_1(object sender, EventArgs e)
        {
            if (Playlist.SelectedIndex != Playlist.Items.Count - 1)
            {
                Playlist.SelectedIndex = Playlist.SelectedIndex + 1;
            }

           
            Krav_Player.GetPlayer().Play(paths[Playlist.SelectedIndex]);


            timetotal.Text = String.Format("{0:00}:{1:00}",
                Krav_Player.GetPlayer().Length / 60, Krav_Player.GetPlayer().Length % 60);
            trackBar1.Maximum = Krav_Player.GetPlayer().Length;
        }

        private void Playlist_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

     
        private void Info_Click(object sender, EventArgs e)
        {
            InfoForm3 f3 = new InfoForm3();
            f3.Show();
        }

        private void Playlist_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Krav_Player.GetPlayer().Play(paths[Playlist.SelectedIndex]);
        }


      
        
       
    }
}
